package com.example.userclient.activity

import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.userclient.Main.ClientInfoHolder
import com.example.userclient.R
import com.example.userclient.adapter.MessageAdapter
import com.example.userclient.adapter.MsgListAdapter
import com.example.userclient.databinding.ActivityMsgListBinding
import java.util.*
import java.util.stream.Collectors

class MessageKey{
    var bookId:Long?=null
    var sendTo:String? = null
    var sendFrom:String? = null

    constructor(){}
    constructor(bookId: Long?, sendTo: String?, sendFrom: String?) {
        this.bookId = bookId
        this.sendTo = sendTo
        this.sendFrom = sendFrom
    }


    override fun equals(other: Any?): Boolean {
        if (this === other) {
            Log.i("tesssst","address is  equal")
            return true
        }
        if (javaClass != other?.javaClass) {
            Log.i("tesssst","javaClass is not equal")
            return false
        }

        other as MessageKey
        if (bookId != other.bookId) {
            Log.i("tesssst","bookId is not equal")
            return false
        }
        if (sendTo == other.sendTo && sendFrom == other.sendFrom)
        {
            Log.i("tesssst","sendTo == other.sendTo && sendFrom == other.sendFrom")
            return true
        }
        if (sendTo == other.sendFrom && sendFrom == other.sendTo){
            Log.i("tesssst","sendTo == other.sendFrom && sendFrom == other.sendTo")
            return true
        }
        Log.i("tesssst","sendTo == other.sendTo ${sendTo == other.sendFrom}&& sendFrom == other.sendFrom ${sendFrom == other.sendFrom}")
        return false
    }
    // java hashcode 和 super hashcode的区别  hash 优先于eqauls  如果hash判断不相等 那就不相等
    /*


    super hashcode  相当于不重写的情况
 2021-04-13 23:41:32.990 13134-13134/com.example.userclient I/tesst: 259735404
2021-04-13 23:41:32.990 13134-13134/com.example.userclient I/tesst: 259735404
2021-04-13 23:41:32.990 13134-13134/com.example.userclient I/tesst: 240458037
2021-04-13 23:41:32.990 13134-13134/com.example.userclient I/tesst: 240458037
2021-04-13 23:41:32.990 13134-13134/com.example.userclient I/tesst: 199758282
2021-04-13 23:41:32.990 13134-13134/com.example.userclient I/tesst: 199758282
2021-04-13 23:41:32.992 13134-13134/com.example.userclient I/tesst: 240458037
2021-04-13 23:41:32.992 13134-13134/com.example.userclient I/tesst: 199758282
2021-04-13 23:41:32.992 13134-13134/com.example.userclient I/tesst: 259735404

    java hashcode     equals 为 true  返回一个结果  equal为false 返回3个结果

2021-04-13 23:43:49.084 14213-14213/com.example.userclient I/tesst: 20485512
2021-04-13 23:43:49.084 14213-14213/com.example.userclient I/tesst: 20485512
2021-04-13 23:43:49.084 14213-14213/com.example.userclient I/tesst: 20485512
2021-04-13 23:43:49.084 14213-14213/com.example.userclient I/tesst: 20485512
2021-04-13 23:43:49.084 14213-14213/com.example.userclient I/tesst: 20485512
2021-04-13 23:43:49.084 14213-14213/com.example.userclient I/tesst: 20485512
2021-04-13 23:43:49.086 14213-14213/com.example.userclient I/tesst: 20485512


    *
    * */

    override fun hashCode(): Int {
        val result = javaClass.hashCode()
        Log.i("tesst","$result")
        return result
    }
    // 如何使得 三个变量

    // 使用java hashcode  变量引用的内存的hashcode 一致


//    override fun hashCode(): Int {
//        var result = bookId?.hashCode() ?: 0
//        Log.i("test","$result")
////        result = 31 * result + (sendTo?.hashCode() ?: 0)
////        result = 31 * result + (sendFrom?.hashCode() ?: 0)
//        return result
//    }


//    override fun hashCode(): Int {
//        var result = sendTo?.hashCode() ?: 0
//        result = 31 * result + (sendFrom?.hashCode() ?: 0)
//        return result
//    }


}


class MsgListActivity : AppCompatActivity() {
    @RequiresApi(Build.VERSION_CODES.N)
    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        val bind =  DataBindingUtil.setContentView<ActivityMsgListBinding>(this, R.layout.activity_msg_list)



        val map  = ClientInfoHolder.personRemark?.stream()
            ?.filter{
                (it.sendFrom == ClientInfoHolder.phone
                        || it.sendTo == ClientInfoHolder.phone   ) && it.sendTo != it.sendFrom
            }
            ?.collect(Collectors.groupingBy {
                val result =  MessageKey(it.book_id,it.sendTo,it.sendFrom)
                Log.i("tessst","${result.hashCode()}")
                result
            })?.toMutableMap()
/**
 *
 *       Log.i("tessst","${result.hashCode()}")
 * 2021-04-13 23:41:32.990 13134-13134/com.example.userclient I/tessst: 259735404
2021-04-13 23:41:32.990 13134-13134/com.example.userclient I/tessst: 240458037
2021-04-13 23:41:32.990 13134-13134/com.example.userclient I/tessst: 199758282
 *
 * */


//        Log.i("MsgListActivity","${map.size}")
//        map.values.forEach{
//            Log.i("MsgListActivity","${it[0].isRead}")
//        }
        MsgListAdapter.dataSet = map

        bind.msgList.layoutManager = LinearLayoutManager(this)

        MsgListAdapter.setOnItemClickListener(object : MsgListAdapter.OnItemClickListener{
            override fun onItemClick(itemView: View?, position: Int) {
                val bookRemark =  MsgListAdapter.dataSet?.values?.elementAt(position)?.get(0)
                val id = bookRemark!!.book_id

                val list = LinkedList<String>()
                ClientInfoHolder.personRemark!!.forEach {
                    if  (  it.book_id == id && !it.isRead && it.sendTo == ClientInfoHolder.phone && it.sendFrom == bookRemark.sendFrom){
                        it.isRead = true
                        list.add(it.id)
                        Log.i("tadd",it.content)
                    }
                }
                ClientInfoHolder.readRemark!!.ids.addAll(
                    list
                )

                for(  i in ClientInfoHolder.liveBook!!){
                    if (bookRemark != null) {
                        if (i.id == bookRemark.book_id){
                            val talkWith:String
                            if (ClientInfoHolder.phone == bookRemark.sendFrom){
                                talkWith = bookRemark.sendTo
                            }else{
                                talkWith = bookRemark.sendFrom
                            }
                            TalkActivity.actionStart2(this@MsgListActivity, i, talkWith)
                            return
                        }
                    }
                }


                Toast.makeText(this@MsgListActivity,"暂无留言",Toast.LENGTH_SHORT).show()
                }
        })

        bind.msgList.adapter = MsgListAdapter



        bind.msgBack.setOnClickListener {
            this.finish()
        }


    }


    override fun onResume() {
        super.onResume()
        MsgListAdapter.notifyDataSetChanged()
    }

    companion object{
        fun actionStart(context: Context){
            val intent = Intent(context, MsgListActivity::class.java)
            context.startActivity(intent)

        }
    }
}